﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.ServiceModel;
using Flower.Services.Data;
using System.Transactions;

namespace Flower.Services.Host
{
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single, 
        ConcurrencyMode = ConcurrencyMode.Multiple, 
        ReleaseServiceInstanceOnTransactionComplete = false,
        TransactionIsolationLevel = IsolationLevel.ReadCommitted
    )]
    internal class Directory : IDirectory
    {
        private readonly IDirectory _directory;

        public Directory(IDirectory directory)
        {
            _directory = directory;
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public string GetPathById(string id)
        {
            return _directory.GetPathById(id);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public DirectoryEntry Get(string what)
        {
            return _directory.Get(what);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public DirectoryEntryInfo GetInfo(string what)
        {
            return _directory.GetInfo(what);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public IList<DirectoryEntry> GetChildren(string parent, DirectoryEntryTypes entryTypes, SortProperty property, SortOrder order, long offset, long limit)
        {
            return _directory.GetChildren(parent, entryTypes, property, order, offset, limit);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public IList<DirectoryEntryInfo> GetChildrenInfo(string parent, DirectoryEntryTypes entryTypes, SortProperty property, SortOrder order, long offset, long limit)
        {
            return _directory.GetChildrenInfo(parent, entryTypes, property, order, offset, limit);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public long CountChildren(string parent, DirectoryEntryTypes entryTypes)
        {
            return _directory.CountChildren(parent, entryTypes);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public DirectoryEntry GetAncestor(string child, int n, DirectoryEntryTypes ancestorEntryTypes)
        {
            return _directory.GetAncestor(child, n, ancestorEntryTypes);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public DirectoryEntryInfo GetAncestorInfo(string child, int n, DirectoryEntryTypes ancestorEntryTypes)
        {
            return _directory.GetAncestorInfo(child, n, ancestorEntryTypes);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public string Save(string parent, DirectoryEntry entry)
        {
            return _directory.Save(parent, entry);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void Move(string what, string where)
        {
            _directory.Move(what, where);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void Remove(string what)
        {
            _directory.Remove(what);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool Exists(string what, DateTime timestamp)
        {
            return _directory.Exists(what, timestamp);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public ICollection<ScriptParameter> ExecuteScript(string script, ICollection<ScriptParameter> args)
        {
            return _directory.ExecuteScript(script, args);
        }
    }
}
